package com.day1020;

import java.awt.image.BufferedImage;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

/**
 *   敌机类
 */

public class EnemyAirplane extends FlySprite implements ActionSprite {
    private int type;//  敌机类型
    private int speed;//  飞机下落速度
    private int blood;//   飞机血量
    private int score;//   分数

    public int getBlood() {
        return blood;
    }

    public void setBlood(int blood) {
        this.blood = blood;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    public EnemyAirplane() {
        this.init();
        Timer timer = new Timer();
        timer.schedule(new EnemyAirPlaneAnimation(),0,200);
    }

    @Override
    public void init() {
        //  创建随机对象
        Random random = new Random();
        //  1-10随机数
        int i = random.nextInt(10) + 1;
        //1:3:6:生成不同的飞机
        if (i % 10 == 0){
            type = 2;
        }else {
            if (i % 3 == 0){
                type = 1;
            }else {
                type = 0;
            }
        }
        //  根据飞机类型，速度不同
        speed = 5 - type;
        if (type == 2){
            score = blood = 10;
        }else {
            if (type == 1){
                score = blood = 5;
            }else {
                score = blood = 1;
            }
        }
        //  根据飞机类型不同，加载相应图片
        super.image = ImageLoadUtils.load("enemy" + type + ".png");
        super.width = super.image.getWidth()/2;
        super.height = super.image.getHeight()/2;
        //x,y
        //敌机从屏幕外进入，因此y轴设定为敌机的负高度
        super.y = -super.height;
        // 随机敌机的X轴
        super.x = random.nextInt(FlyFrame.WIDTH - super.width);
    }

    @Override
    public void destroy() {
        FlyPanel.enemys.remove(this);
    }

    @Override
    public void down() {
        super.y += speed;
        if (super.y > FlyFrame.HEIGHT){
            this.destroy();
        }
    }

    class EnemyAirPlaneAnimation extends TimerTask{
        int num;
        @Override
        public void run() {
            if (isDead()){
                num ++ ;
                BufferedImage image = ImageLoadUtils.load("enemy" + type + "_down" + num + ".png");

                if ((type != 4 && num == 4) || (type == 2 && num == 6)){
                    num = 0;
                    destroy();
                }
                EnemyAirplane.super.image = image;

            }
        }
    }
}
